<template>
  <div class="uploadImage">
    <el-upload
      v-if="!imgUrl"
      action="https://jsonplaceholder.typicode.com/posts/"
      list-type="picture-card"
      :before-upload="beforeupload"
      :http-request="httprequest"
    >
      <i class="el-icon-plus" />
    </el-upload>
    <div v-else class="myimg">
      <!-- <img :src="imgUrl" alt=""> -->
      <el-image :src="imgUrl" :preview-src-list="[imgUrl]" />
      <i class="el-icon-close myclose-i" @click="removeImg" />
      <el-progress
        v-show="isShowprogress"
        :percentage="percentage"
        :show-text="false"
      />
    </div>
    <!-- <el-dialog title="提示" :visible.sync="showDialog">
      <div class="preview">
        <img :src="imgUrl" alt="">
      </div>
    </el-dialog> -->
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
export default {
  props: {
    imgUrl: String
  },
  data() {
    return {
      showDialog: false,
      cos: null,
      percentage: 50,
      isShowprogress: false
    }
  },
  created() {
    this.cos = new COS({
      SecretId: 'AKIDkIelcDXcRzLRIsBDzVkjRu61f3A0Zsuf', // 身份识别 ID
      SecretKey: 'VjhGafU2JJ4XkfJhncASTGn56rLgkX3I' // 身份密钥
    })
  },
  methods: {
    removeImg() {
      this.$emit('update:imgUrl', '')
      //   console.log(1)
    },
    beforeupload(file) {
      //   console.log(file)
      const type = file.type === 'image/jpeg' || file.type === 'image/png'
      if (!type) {
        this.$message.error('上传失败，文件格式不对')
      }
      const size = file.size / 1024 / 1024 < 2
      if (!size) {
        this.$message.error('上传失败，文件大小不能大于2M')
      }
      return type && size
    },
    httprequest(file) {
      this.isShowprogress = true
      var myfile = file.file
      //   console.log(file)
      this.cos.putObject(
        {
          Bucket: 'people-1258253431' /* 必须 */,
          Region: 'ap-shenzhen-fsi' /* 存储桶所在地域，必须字段 */,
          Key: myfile.name /* 必须 */,
          StorageClass: 'STANDARD',
          Body: myfile, // 上传文件对象
          onProgress: progressData => {
            // 上传过程中的实时进度对象
            // console.log(progressData)
            this.percentage = progressData.percent * 100
          }
        },
        (err, data) => {
          //   上传成功后的返回值
          //   上传出错的错误信息
          //   console.log(err || data)
          if (err) {
            this.$message.error(err.message)
            return
          }
          this.$emit('update:imgUrl', 'http://' + data.Location)
          //   console.log(data)
          setTimeout(() => {
            // 关闭进度条
            this.isShowprogress = false
          }, 1000)
        }
      )
    }
  }
}
</script>

<style lang="scss" scopend>
.uploadImage {
  .myimg {
    width: 150px;
    height: 150px;
    position: relative;
    img {
      width: 150px;
      height: 150px;
    }
    .myclose-i {
      position: absolute;
      top: 0;
      right: 0;
      width: 20px;
      height: 20px;
      background: rgba(0, 0, 0, 0.5);
      text-align: center;
      line-height: 20px;
      border-radius: 0 0 0 18px;
      // border-radius: 10px;
      display: none;
      font-size: 16px;
      color: #fff;
    }
    &:hover .myclose-i {
      display: block;
      cursor: pointer;
    }
  }
}
</style>
